/*
*********************
    Object实现
*********************
*/

#include "../include/object.h"

static const ClassInfo object_class_info = {
    .name = "Object",
    .super = NULL,
    .instnce_size = sizeof(Object)
};

static const ObjectVTable object_vtable = {
    .destroy = Object_destroy,
    .toString = Object_toString
}

Object* retain(Object* obj){
    if (obj)
        obj->ref_count++;
    return obj;
}

void release(Object* obj) {
    if (!obj)
        return;
    obj->ref_count--;
    if (obj->ref_cout <= 0) {
        obj->vptr->destroy(obj);
    }
}

void Object_destroy(Object* self){
    free(self);
}

const char* Object_toString(Object* self) {
    static char buf[64];
    snprintf(buf, sizeof(buf), "%s@%p", self->clazz->name, self);
    return buf;
}

int instanceof(const Object* obj, const ClassInfo* type) {
    if (!obj || !type)
        return 0;
    const ClassInfo* current = obj->clazz;
    while (current) {
        if (current == type)
            return 1;
        current = current->super;
    }
    return 0;
}

const ClassInfo* get_class(const Object* obj) {
    return obj ? obj->clazz : NULL;
}